//
//  HeartRateHistoryDetailController.swift
//  DaRings
//
//  Created by HFY on 2022/11/21.
//

import UIKit
import MYModule

class SingleHeartRateDetailController: BaseTableViewController {
    
    
    
    var provider: SingleHeartRateProvider? {
        
        didSet {
            
            tableView.reloadData()
        }
        
    }
    
    var heartRateModel: SingleHeartRateModel? {
        
        didSet {
            
            guard let heartRateModel = heartRateModel else {
                return
            }

            provider = SingleHeartRateProvider(heartRateModel: heartRateModel)
            
        }
        
    }
    
    
    var showRightItem: Bool = true

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavigationBar()
        
    }
    
    func setNavigationBar() {
        
        navigation.bar.backgroundColor = UIColor.background
        navigation.item.title = "heart_rate_title".language
        
        
        if showRightItem {
            navigation.item.rightBarButtonItem = historyBarButtonItem
        }
        
    }
    
    override func makeUI() {
        super.makeUI()
        
        view.addSubview(tableView)
        
        let top = UIApplication.shared.statusBarFrame.height + self.navigation.bar.height
        
        tableView.backgroundColor =  UIColor.darkBackground
        tableView.register(HeartRateStateCell.self, forCellReuseIdentifier: HeartRateStateCell.reuseIdentifier)
        tableView.register(HeartRateDataCell.self, forCellReuseIdentifier: HeartRateDataCell.reuseIdentifier)
        tableView.register(HeartRateSevenTimesCell.self, forCellReuseIdentifier: HeartRateSevenTimesCell.reuseIdentifier)
        
        tableView.dataSource = self
        
        
        tableView.snp.makeConstraints { make in
            
            make.top.equalTo(top)
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            
        }
        
    }
    
    
    override func bindViewModel() {
        super.bindViewModel()
        
        
        historyBarButtonItem.rx.tap.subscribe(onNext: {[weak self] in
            
            self?.pushToHistoryListController()
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    public lazy var historyBarButtonItem: UIBarButtonItem = {
        let historyBarButtonItem = UIBarButtonItem()
        historyBarButtonItem.image = UIImage(named: "ic_data")
        return historyBarButtonItem
    }()
    
}

extension SingleHeartRateDetailController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: HeartRateStateCell.reuseIdentifier, for: indexPath) as! HeartRateStateCell
            
            cell.detail = provider?.detail
            
            return cell
            
        }
        
        if indexPath.row == 1 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: HeartRateDataCell.reuseIdentifier, for: indexPath) as! HeartRateDataCell
            
            
            return cell
            
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: HeartRateSevenTimesCell.reuseIdentifier, for: indexPath) as! HeartRateSevenTimesCell
        
        cell.heartRateModels = provider?.lastSevenTimeHearts
        return cell
        
    }
    
    
}

extension SingleHeartRateDetailController {
    
    func pushToHistoryListController() {
        
        let vc = HistoryController()
        vc.showType = .heart
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
}
